package com.hzd.web;

import ablaze.framework.api.result.ExecuteResult;
import ablaze.framework.config.SystemConfig;
import ablaze.framework.exception.BaseException;
import ablaze.framework.web.DataController;
import com.hzd.entity.lol.LolUser;
import com.hzd.service.WxService;
import com.hzd.service.lol.LolRandom;
import com.hzd.service.lol.LolService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author kent
 * @date 15/11/9.
 */
@Controller
@RequestMapping(value = "/lol")
public class LolController extends DataController {

    private static final Logger LOGGER = LoggerFactory.getLogger(LolController.class);

    @Autowired
    private LolService lolService;

    @Autowired
    private WxService wxService;

    //微信域名
    private static final String DOMAIN_NAME = SystemConfig.getString("wx.domain.name");

    private static final String SHARE_IMG_URL = DOMAIN_NAME + "/lol/static/lol/img/share_img.jpg";


    /**
     * 微信授权回调页面
     *
     * @param code
     * @param state
     * @return
     */
    @RequestMapping(value = "/oAuthCallBack")
    public String oAuthCallBack(String code, String state) {

        if (isEmpty(code)) {

            LOGGER.info("微信授权回调未返回code");

            throw new BaseException("微信授权回调未返回code");
        }

        //分享者openId
        String openid = wxService.getOpenId(code);

        if (isEmpty(openid)) {
            LOGGER.info("获取openid失败");

            throw new BaseException("获取openid失败");
        }

        return "redirect:/lol/mainPage?openid=" + openid;
    }

    /**
     * 活动主页面
     *
     * @param openid
     * @param model
     * @return
     */
    @RequestMapping(value = "/mainPage")
    public String mainPage(String openid, Model model) {

        model.addAttribute("openid", openid);

        model.addAttribute("config",wxService.getJsConfig(wxService.getCurrentURL(getRequest())))
                .addAttribute("shareImgUrl",SHARE_IMG_URL)
                .addAttribute("shareUrl", wxService.getShareUrl(DOMAIN_NAME + "/lol/lol/oAuthCallBack", null))
                .addAttribute("joinUrl",wxService.getShareUrl(DOMAIN_NAME + "/lol/lol/uploadScoreOAuthCallBack",null));


        return "/lol/Main";
    }

    /**
     * 上传积分授权回调接口
     *
     * @param code
     * @param state
     * @return
     */
    @RequestMapping(value = "/uploadScoreOAuthCallBack")
    public String uploadScoreOAuthCallBack(String code, String state) {

        if (isEmpty(code)) {

            LOGGER.info("微信授权回调未返回code");

            throw new BaseException("微信授权回调未返回code");
        }

        //分享者openId
        String openid = wxService.getOpenId(code);

        if (isEmpty(openid)) {
            LOGGER.info("获取openid失败");

            throw new BaseException("获取openid失败");
        }
        return "redirect:/lol/uploadScorePage?openid=" + openid;
    }

    @RequestMapping(value = "/uploadScorePage")
    public String uploadScorePage(String openid, Model model) {

        model.addAttribute("openid", openid);

        model.addAttribute("config",wxService.getJsConfig(wxService.getCurrentURL(getRequest())))
                .addAttribute("shareImgUrl", SHARE_IMG_URL)
                .addAttribute("isParticipate",!lolService.isNotParticipate(openid).success())
                .addAttribute("randomShareNum", LolRandom.randomShareNum())
                .addAttribute("shareUrl", wxService.getShareUrl(DOMAIN_NAME + "/lol/lol/oAuthCallBack", null));

        return "/lol/UploadScore";
    }

    /**
     * 参加活动
     *
     * @param lolUser
     * @return
     */
    @RequestMapping(value = "/participate")
    @ResponseBody
    public ExecuteResult participate(LolUser lolUser) {

        return lolService.participate(lolUser);
    }

    @RequestMapping(value = "/afterShare")
    @ResponseBody
    public ExecuteResult afterShare(String openid) {

        return lolService.sendRate(openid);
    }

    @RequestMapping(value = "/sendVerifyCode")
    @ResponseBody
    public ExecuteResult sendVerifyCode(String mobile){

        return lolService.sendVerifyCode(mobile);
    }

}
